/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import { DeviceEventEmitter, View, StatusBar } from 'react-native';
import { createStackNavigator } from 'react-navigation';
import { Provider } from 'react-redux';
import { SCREENS } from 'bee/constants';
import Screens from './screens';
import configStore from './configureStore';

const AppNavigator = createStackNavigator(Screens, {
  // initialRouteName: 'MakePlan',
  initialRouteName: 'Main',
});

// gets the current screen from navigation state
function getCurrentRouteName(navigationState) {
  if (!navigationState) {
    return null;
  }
  const route = navigationState.routes[navigationState.index];
  // dive into nested navigators
  if (route.routes) {
    return getCurrentRouteName(route);
  }
  return route.routeName;
}

const store = configStore();

export default class App extends Component {
  render() {
    return (
      <View style={{ flex: 1 }}>
        <StatusBar hidden={false} translucent backgroundColor="#00000000" />
        <Provider store={store}>
          <AppNavigator
            onNavigationStateChange={(prevState, currentState) => {
              const currentScreen = getCurrentRouteName(currentState);
              DeviceEventEmitter.emit('NavigationStateChange', currentScreen);
            }}
          />
        </Provider>
      </View>
    );
  }
}
